<template>
	<view class="content">
		<button type="primary" @tap="onScan">扫描蓝牙</button>
		<view class="wrap" v-for="device in deviceList" :key="device.mac">
			<text> {{ device.mac}} -- {{ device.name}} </text>
			<button v-if="linkMac != device.mac" type="primary" size="mini" @tap="onLink(device.mac)">连接</button>
			<button v-else type="warn" size="mini" @tap="onUnlink(device.mac)">断开</button>
		</view>
		<view v-show="linkMac">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						{{isStartMeasure?'测量类型':'请选择测量类型'}}
					</view>
					<view class="uni-list-cell-db">
						<picker :disabled="isStartMeasure" @change="onMessageTypeChange" :value="mTypeIndex"
							:range="mTypeArray">
							<view class="uni-input">【 {{mTypeArray[mTypeIndex]}} 】</view>
						</picker>
					</view>
				</view>
			</view>
			<button v-if="!isStartMeasure" type="primary" @tap="onMeasure">开始测量</button>
			<button v-else type="warn" @tap="onStopMeasure">停止测量</button>
			<view class="uni-list">
				<view class="uni-list-cell">
					<text class="tw">{{result}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * 问题：
	 * 1、sdk中可以单独调用searchBt，但是如果要调用measure必须同时调用searchBt才会有返回值
	 * 2、searchBt返回值会是空数组，有时有数据有时没数据
	 * 3、只有5中单一数据返回，没有折线图
	 */
	export default {
		data() {
			const dcSensorManager = uni.requireNativePlugin("DCloud-SensorManager");
			return {
				dcSensorManager,
				mTypeArray: ['温度', '转速', '加速度', '速度', '位移'],
				mTypeValue: [1, 2, 3, 4, 5],
				mTypeIndex: 0,
				deviceList: [],
				linkMac: null,
				result: null,
				isStartMeasure: false
			}
		},
		beforeDestroy() {
			console.log('onUnlink')
			this.onUnlink()
		},
		methods: {
			// 扫描
			onScan() {
				// 方式一：监听配对
				// this.searchBt({
				// 	loading: true
				// })
				// 方式二：只读取已配对蓝牙设备
				// 通过手机蓝牙手动配对后，这里直接读取所有配对蓝牙即可
				this.getBondedDevices()
			},
			// 连接
			onLink(mac) {
				this.onUnlink()
				this.$nextTick(() => {
					this.dcSensorManager.connectBt(mac, pass => {
						if (pass) {
							this.linkMac = mac
						} else {
							uni.showToast({
								icon: 'none',
								duration: 1000,
								title: '连接失败'
							})
						}
					})
				})
			},
			// 断开
			onUnlink() {
				this.isStartMeasure = false
				this.linkMac && this.dcSensorManager.destroy()
				this.linkMac = null
			},
			// 测量类型改变
			onMessageTypeChange(e) {
				this.mTypeIndex = e.detail.value
			},
			// 循环测量
			onMeasure() {
				this.isStartMeasure = true
				this.result = '处理中'
				this.$nextTick(() => {
					this.measure()
				})
			},
			// 停止测量
			onStopMeasure() {
				this.isStartMeasure = false
			},
			// 调用sdk中扫描接口，蓝牙连接结果和测量结果都是在这个回调result中返回
			// searchBt(options = {}) {
			// 	const {
			// 		loading
			// 	} = options
			// 	loading && uni.showLoading({
			// 		mask: true,
			// 		title: '处理中'
			// 	})
			// 	this.dcSensorManager.searchBt(result => {
			// 		console.log(result)
			// 		loading && uni.hideLoading()
			// 		// 如果是蓝牙设备列表，["DC:0D:30:66:E8:2C--AS1000"]
			// 		if (Array.isArray(result) && result.length > 0) {
			// 			this.deviceList = result.map(it => {
			// 				const [mac, name] = it.split('--')
			// 				return {
			// 					mac,
			// 					name
			// 				}
			// 			})
			// 		} else {
			// 			this.searchBt({
			// 				loading: true
			// 			})
			// 		}
			// 	})
			// },
			// 调用sdk中获取已配对蓝牙
			getBondedDevices() {
				this.dcSensorManager.getBondedDevices(result => {
					if (Array.isArray(result) && result.length > 0) {
						this.deviceList = result.map(it => {
							const [mac, name] = it.split('--')
							return {
								mac,
								name
							}
						})
						uni.showToast({
							icon: 'none',
							duration: 1000,
							title: '刷新完成'
						})
					} else {
						this.deviceList = []
						uni.showToast({
							icon: 'none',
							duration: 3000,
							title: '无配对设备，请手动连接蓝牙配对再扫描'
						})
					}
					if (this.linkMac && this.deviceList.every(it => it.mac != this.linkMac)) {
						this.onUnlink()
					}
				})
			},
			// 调用sdk中测量接口
			measure() {
				if (!this.linkMac) return
				const mType = this.mTypeValue[this.mTypeIndex]
				// 4个参数：mac地址，类型，测量多少秒内平均值，第一次连接蓝牙后等待多少秒再测量
				// console.log(this.linkMac, mType)
				this.dcSensorManager.measure(this.linkMac, mType, 0.2, 0.2, result => {
					this.result = result
					if (this.isStartMeasure) {
						this.$nextTick(() => {
							this.measure()
						})
					}
				})
			}
		},
	}
</script>

<style>
	.content {
		padding: 50rpx 0;
	}

	.wrap {
		margin-top: 30rpx;
		padding: 20rpx 0 20rpx 20rpx;
		border: 1px solid #ccc;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.uni-list {
		padding: 30rpx;
	}

	.uni-list-cell {
		display: flex;
		padding: 20rpx;
		align-items: center;
	}

	.uni-list-cell-db {
		margin-left: 30rpx;
	}

	.tw {
		width: 100%;
		display: inline-block;
		white-space: pre-wrap;
		word-wrap: break-word;
		height: auto;
	}
</style>